package com.mall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


import com.mall.common.utils.BeanUtilKit;
import com.mall.product.entity.AttrAttrgroupRelationEntity;
import com.mall.product.entity.AttrEntity;
import com.mall.product.service.AttrAttrgroupRelationService;
import com.mall.product.service.AttrService;
import com.mall.product.service.CategoryService;
import com.mall.product.vo.AttrGroupVO;
import com.mall.product.vo.AttrVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.mall.product.entity.AttrGroupEntity;
import com.mall.product.service.AttrGroupService;
import com.mall.common.utils.PageUtils;
import com.mall.common.utils.R;



/**
 * 属性分组
 *
 * @author xzt
 * @email xzt@163.com
 * @date 2024-08-10 15:54:08
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrAttrgroupRelationService attrgroupRelationService;

    @RequestMapping("/{catelogId}/withattr")
    public R withAttrList(@PathVariable(value = "catelogId") Long catelogId){

        List<AttrGroupEntity> attrGroupEntityList = attrGroupService.getAttrGroupByCatelogId(catelogId);
        List<AttrGroupVO>  attrGroupVOList = BeanUtilKit.copyObjectList(attrGroupEntityList, AttrGroupVO.class);
        attrGroupVOList.forEach(item -> {
           Long groupId =  item.getAttrGroupId();
           List<AttrVO> attrVOList = attrService.getAttrVOByGroupId(groupId);
            item.setAttrs(attrVOList);
        });

        return R.ok().put("data", attrGroupVOList);
    }

    @RequestMapping("/{groupId}/noattr/relation")
    public R noRelationAttrList(@RequestParam Map<String, Object> params , @PathVariable(value = "groupId") Long groupId){
        PageUtils page = attrGroupService.queryNoRelationAttrListPage(params,groupId);

        return R.ok().put("page", page);
    }

    @PostMapping("/attr/relation")
    public R saveAttrRelationList(@RequestBody  List<AttrAttrgroupRelationEntity> list){
        attrgroupRelationService.saveBatch(list);
        return R.ok();
    }

    /**
     * 列表
     */

    @GetMapping("/{groupId}/attr/relation")
    public R attrRelationList(@PathVariable("groupId") Long groupId){
        List<AttrEntity> attrEntityList = attrGroupService.queryPage(groupId);
        return R.ok().put("data", attrEntityList);
    }
    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params , @PathVariable(value = "catelogId") Long catelogId){
        PageUtils page = attrGroupService.queryPage(params,catelogId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

        attrGroup.setCatelogPath(categoryService.getCatelogPath(attrGroup.getCatelogId()));

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除 attr/relation/delete
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    @RequestMapping("/attr/relation/delete")
    public R delete(@RequestBody List<AttrAttrgroupRelationEntity> attrGroupIdList){

        attrgroupRelationService.removeAttrGourpList(attrGroupIdList);


        return R.ok();
    }

}
